package com.chinasoft.controller;

import com.chinasoft.pojo.*;
import com.chinasoft.service.Impl.TeacherServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;
@RestController
@CrossOrigin
@RequestMapping("/teacher")
public class TeacherController {
    @Autowired
    TeacherServiceImpl teacherService;

//    @PostMapping("/list")
//    public List<TeacherList> list() {
//
//        return teacherService.findAll();
//    }

    @PostMapping("/list1/{current}/{size}")
    public Result list1(@PathVariable Integer current, @PathVariable Integer size) {
        PageItem<TeacherList> brandPageItem = teacherService.find1(current, size);

        Result result = new Result(brandPageItem, "查询成功");
        return result;
    }
    @PostMapping("/list/state/{state}/{current}/{size}")
    public Result searchtype(@PathVariable String state,@PathVariable Integer current, @PathVariable Integer size) {
        PageItem<TeacherList> brandPageItem = teacherService.getByState(state,current,size);
        Result result = new Result(brandPageItem, "查询成功");
        return result;
    }

    @GetMapping("/search/{id}")
    public Result getOne(@PathVariable Integer id) {
        TeacherList byId = teacherService.getById(id);
        if (byId == null) {
            return Result.fail(404, "查询不到该数据");
        }
        Result result = new Result(byId, "查询成功");
        return result;
    }
    @PostMapping("/search/{classtype}")
    public List<TeacherList> getClass(@PathVariable String classtype) {
        return teacherService.getByClass(classtype);
    }

    @PostMapping("/searchlist/{teachername}")
    public List<TeacherList> getTeacher(@PathVariable String teachername) {
        return teacherService.getByTeacher(teachername);
    }

    @PostMapping("/searchlistall/{teachername}")
    public List<TeacherList> getTeacherAll(@PathVariable String teachername) {
        return teacherService.getByTeacherAll(teachername);
    }

    @PostMapping("/list/updateSoild")
    public Result updateCode(@RequestBody TeacherList OrderUpdate) {
        boolean updateCode = teacherService.updateSoild(OrderUpdate.getId(), OrderUpdate.getSoild());
        if(updateCode == true) {
            return new Result(null, "修改成功");
        }else{
            return Result.fail(500, "修改失败");
        }
    }

    @PostMapping("/list/updateState")
    public Result updateState(@RequestBody TeacherList OrderUpdate) {
        boolean updateCode = teacherService.updateState(OrderUpdate.getId(), OrderUpdate.getState());
        if(updateCode == true) {
            return new Result("success", "修改成功");
        }else{
            return Result.fail(500, "修改失败");
        }
    }

    @PostMapping("/addlist")
    // ResponseBody 将json字符串 转换为java 列表
    public Result insertRec(@RequestBody TeacherList brand) {
        boolean insertBrand = teacherService.addlist(brand);
        if (insertBrand) {
            return new Result(brand, "添加成功");
        } else {
            return Result.fail(500, "添加失败");
        }
    }

    //查询课程类型的总销量
    @PostMapping("/dailnum")
    @ResponseBody
    public Result<TeacherCount> getDailSalesByDays() {
        List<TeacherCount> result = teacherService.findgetNum();
        return new Result(result, "查询成功");
    }

    @PostMapping("/dailmonth")
    @ResponseBody
    public Result<TeacherCount> getDail() {
        List<TeacherCount> result = teacherService.find();
        return new Result(result, "查询成功");
    }

    @PostMapping("/searchBy")
    public List<TeacherList> listBy(@RequestBody TeacherList brand) {
        return teacherService.getBy(brand);
    }

    @PostMapping("/searchList")
    public List<TeacherList> list(@RequestBody TeacherList brand) {
        return teacherService.getfind(brand);
    }

    @DeleteMapping("/delete/{id}")
    public Result delete(@PathVariable Integer id) {
        boolean delete = teacherService.deleteById(id);
        if (delete) {
            return new Result(null, "删除成功");
        } else {
            return Result.fail(500, "删除失败");
        }
    }

    @PostMapping("/delete1")
    public Result delete1(@RequestBody TeacherList brand) {
        boolean delete = teacherService.delete1(brand.getId(),brand.getClasstype());
        if (delete) {
            return new Result(null, "删除成功");
        } else {
            return Result.fail(500, "删除失败");
        }
    }

    @PostMapping("/update")
    public Result update(@RequestBody TeacherList teacherList) {
        boolean update = teacherService.update(teacherList);
        if (update) {
            return new Result(null, "修改成功");
        } else {
            return Result.fail(500, "修改失败");
        }
    }
    @PostMapping("/list/searchTime")
    public Result updateTime(@RequestBody TeacherList OrderUpdate) {
        boolean updateCode = teacherService.getTime(OrderUpdate.getTeachername(), OrderUpdate.getClasstype(), OrderUpdate.getTime());
        if(updateCode) {
            return new Result(200, "查找成功");
        }else{
            return new Result(500, "查找成功");
        }
    }
}
